package org.jchains.tools;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.jchains.CORBA.PermissionTransferPackage.EnvironmentEntry;
import org.jchains.receiver.Perm;
import org.jchains.receiver.PolicyFile;

public class PolicyGen {
	
	public PolicyFile getFile(String a) throws Exception {
		int i = 0;
		int anzahl = 0;
		PolicyFile pf = new PolicyFile("",new EnvironmentEntry[] {});
		FileInputStream  f = null;
		try {
			f = new FileInputStream(a);
			//pf = new PolicyFile("", null);
			int lauf = 0;
			BufferedReader br = new BufferedReader(new InputStreamReader(f));
			boolean ende = false;
//			int delta = 1;

			while (!ende) {
				String z = br.readLine();

				Perm pp = null;

				try {

					pp = new Perm(z);

				} catch (Exception e) {
					ende = true;
				}
				// if (pp instanceof Perm) {
				if (!ende) {
					pf.add(pp);
					lauf++;
				} else {
					// System.out.println(pp.getClass() + ":" + pp);
				}
				i++;
				anzahl = i;
			}
			f.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				f.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		anzahl = i;
		System.out.println("Import ended:" + i);
		return pf;

	}
	public static void main(String[] a) throws Exception {
		String z = a[0];
		PolicyGen pg = new PolicyGen();
		PolicyFile pf = pg.getFile(z);
		System.out.println(pf.getContent(false));
	}
}
